const vscode = require("vscode");
const GlobalVriable = require("../utils/globalVriable");

/**
 * 自动补全
 * @param {any} document
 * @param {any} position
 * @param {any} token
 * @param {any} context
 */
// eslint-disable-next-line no-unused-vars
function provideCompletionItems(document, position, token, context) {
    const line = document.lineAt(position);

    // 只截取到光标位置为止，防止一些特殊情况
    const lineText = line.text.substring(0, position.character);
    let functionList = GlobalVriable.getAjjlFunctionLibrary(document);

    // 添加提示
    let completionItems = [];
    functionList.forEach((functionObj) => {
        if (new RegExp("Lib.$", "g").test(lineText)) {
            completionItems.push(new vscode.CompletionItem(functionObj["class"], vscode.CompletionItemKind.Interface));
        }
        if (new RegExp("Lib." + functionObj["class"] + ".$", "g").test(lineText)) {
            functionObj["infos"].forEach((functionInfo) => {
                let showText = functionInfo["name"] + "(" + functionInfo["params"].join(", ") + ")";
                completionItems.push(new vscode.CompletionItem(showText, vscode.CompletionItemKind.Method));
            });
        }
    });
    return completionItems;
}

/**
 * 光标选中当前自动补全item时触发动作，一般情况下无需处理
 * @param {any} item
 * @param {any} token
 */
// eslint-disable-next-line no-unused-vars
function resolveCompletionItem(item, token) { return null; }

/**
 *  注册代码建议提示，只有当按下“.”时才触发
 * @param {any} context
 */
module.exports = function(context) {
    context.subscriptions.push(
        vscode.languages.registerCompletionItemProvider("q", { provideCompletionItems, resolveCompletionItem }, ".")
    );
};